//
//  STTestParentController.swift
//  iPhone
//
//  Created by Toj on 2/14/23.
//

import UIKit

private var titleIndex = 0

// MARK: - 懒加载(属性), 变量, 初始化init
class STTestParentController: UIViewController {
    
    var isShowAll = true
    
    /* TitleView */
    private lazy var backgroundView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.black.tt_alpha(0.5)
        let tap = UITapGestureRecognizer(target: self, action: #selector(didTapBackground))
        view.addGestureRecognizer(tap)
        return view
    }()
    
    /* TitleView */
    private lazy var titleLabel: UILabel = {
        let view = UILabel()
        view.textAlignment = .center
        view.textColor = .black
        return view
    }()
    
    /* noneButton */
    private lazy var noneButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试None", for: .normal)
        view.addTarget(self, action: #selector(didTapTestNone), for: .touchUpInside)
        return view
    }()
    
    /* fullScreenButton */
    private lazy var fullScreenButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试FullScreen", for: .normal)
        view.addTarget(self, action: #selector(didTapTestFullScreen), for: .touchUpInside)
        return view
    }()
    
    /* pageSheetButton */
    private lazy var pageSheetButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试PageSheet", for: .normal)
        view.addTarget(self, action: #selector(didTapTestPageSheet), for: .touchUpInside)
        return view
    }()
    
    /* formSheetButton */
    private lazy var formSheetButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试FormSheet", for: .normal)
        view.addTarget(self, action: #selector(didTapTestFormSheet), for: .touchUpInside)
        return view
    }()

    /* currentContextButton */
    private lazy var currentContextButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试CurrentContext", for: .normal)
        view.addTarget(self, action: #selector(didTapTestCurrentContext), for: .touchUpInside)
        return view
    }()
    
    /* customButton */
    private lazy var customButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试Custom", for: .normal)
        view.addTarget(self, action: #selector(didTapTestCustom), for: .touchUpInside)
        return view
    }()
    
    /* overFullScreenButton */
    private lazy var overFullScreenButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试OverFullScreen", for: .normal)
        view.addTarget(self, action: #selector(didTapTestOverFullScreen), for: .touchUpInside)
        return view
    }()

    /* overCurrentContextButton */
    private lazy var overCurrentContextButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试OverCurrentContext", for: .normal)
        view.addTarget(self, action: #selector(didTapTestOverCurrentContext), for: .touchUpInside)
        return view
    }()
    
    /* popoverButton */
    private lazy var popoverButton: UIButton = {
        let view = UIButton()
        view.setTitle("测试Popover", for: .normal)
        view.addTarget(self, action: #selector(didTapTestPopover), for: .touchUpInside)
        return view
    }()
    
    var isShow = false
    
    deinit {
        guard isShow else { return }
        titleIndex -= 1
    }
}

// MARK: - 生命周期
extension STTestParentController {
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        setupMakeInitialize()
        setupMakeAddSubviews()
        setupMakeLayoutSubviews()
    }
    
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        if isShow { return }
        
        isShow = true
        titleIndex += 1
        titleLabel.text = "当前页面: \(titleIndex)"
    }
}

// MARK: - 点击事件
@objc private extension STTestParentController {
    
    func didTapBackground() {
        dismiss(animated: true)
    }
    
    func present(modal presentationStyle: UIModalPresentationStyle) {
        let vc = STTestParentController()
        vc.isShowAll = false
        vc.modalPresentationStyle = presentationStyle
        
        let rootViewController = UIViewController.vc_topViewController
        rootViewController?.present(vc, animated: false)
        
//        print("-->toj: presentingViewController: \(vc.presentingViewController)")
//        print("-->toj: presentedViewController: \(presentedViewController)")
//        print("-->toj: parent: \(parent)")
    }
    
    /// none: 报错
    func didTapTestNone() {
        dismiss(animated: true)
        // present(modal: .none)
    }
    
    /// fullScreen
    func didTapTestFullScreen() {
       present(modal: .fullScreen)
        DispatchQueue.main.asyncAfter(deadline: .now()+0.25) {
            let loginVC = UIFlowLightViewController()
            let rootViewController = UIApplication.shared.keyWindow?.rootViewController
            rootViewController?.present(loginVC, animated: true)
        }
        
    }
    
    /// pageSheet
    func didTapTestPageSheet() {
        let vc = UIViewController()
        vc.view.backgroundColor = .random
        UIViewController.vc_currentNavigation?.pushViewController(vc, animated: true)
        
//        present(modal: .pageSheet)
    }
    
    /// formSheet
    func didTapTestFormSheet() {
        present(modal: .formSheet)
    }
    
    /// currentContext
    func didTapTestCurrentContext() {
        present(modal: .currentContext)
    }
    
    /// custom
    func didTapTestCustom() {
        present(modal: .custom)
    }
    
    /// overFullScreen
    func didTapTestOverFullScreen() {
        present(modal: .overFullScreen)
    }
    
    /// overCurrentContext
    func didTapTestOverCurrentContext() {
        present(modal: .overCurrentContext)
    }
    
    /// popover
    func didTapTestPopover() {
        present(modal: .popover)
    }
}

// MARK: - Private
private extension STTestParentController {
    func test() {
        print("-->toj: self: \(self.cm_objectAddress)")
        print("-->toj: parent: vc isPresent: \(st_isPresent)")
        
        if let nav = navigationController {
            print("-->toj: parent: nav isPresent: \(nav.st_isPresent)")
        } else {
            print("-->toj: parent: nav isPresent: nil")
        }
        
        if let tab = UIApplication.shared.delegate?.window??.rootViewController as? UITabBarController {
            print("-->toj: parent: tab isPresent: \(tab.st_isPresent)")
        }
        else {
            print("-->toj: parent: tab isPresent: nil")
        }
    }
}

// MARK: - 初始化设置
private extension STTestParentController {
    
    func setupMakeAddSubviews() {
        view.addSubview(backgroundView)
        
        view.addSubview(titleLabel)
        view.addSubview(noneButton)
        view.addSubview(fullScreenButton)
        view.addSubview(pageSheetButton)
        view.addSubview(formSheetButton)
        view.addSubview(currentContextButton)
        view.addSubview(customButton)
        view.addSubview(overFullScreenButton)
        view.addSubview(overCurrentContextButton)
        view.addSubview(popoverButton)
    }
    
    func setupMakeLayoutSubviews() {
        var top = CGFloat.sts_barHeight + 20
        titleLabel.frame = CGRect(x: 0, y: top, width: .sr_width, height: 20)
        backgroundView.frame = view.bounds
        let buttons = [
            noneButton,
            fullScreenButton,
            pageSheetButton,
            formSheetButton,
            currentContextButton,
            customButton,
            overFullScreenButton,
            overCurrentContextButton,
            popoverButton
        ]
        
        let clm = 1
        let clmFloat = CGFloat(clm)
        let width: CGFloat = 300
        let height: CGFloat = 44.ui_mscale
        let margin = (CGFloat.sr_width - width * clmFloat) / (clmFloat + 1)
        var x: CGFloat = 0
        var y: CGFloat = 0
        var bIndex = 0
        let topMargin: CGFloat =  20
        top += 20
        buttons.forEach { button in
            button.backgroundColor = .random
            button.titleLabel?.font = 13.to_boldFont
            x = margin + CGFloat(bIndex % clm) * (width + margin)
            y = top + CGFloat(bIndex / clm) * (height + topMargin)
            button.snp.makeConstraints { make in
                make.left.equalTo(x)
                make.top.equalTo(y)
                make.width.equalTo(width)
                make.height.equalTo(height)
            }
            bIndex += 1
        }
        
        guard !isShowAll else {
            return
        }
        let count = buttons.count
        let random = arc4random_uniform(UInt32(count))
        
        bIndex = 0
        for button in buttons {
            button.isHidden = bIndex != random
            bIndex += 1
        }
    }
    
    func setupMakeInitialize() {
        view.backgroundColor = isShowAll ? .white : .clear
        print("-->toj: isPresent: \(st_isPresent)")
    }
}
